﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Common;
using System.Data;

namespace Catering.UserTraining.SlrCompanyTrainning
{
    public partial class staff_lessons_learn : UserIsNull
    {
        public int i_InitPage = 1;
        public int i_LessionID = 0;
        public string s_FilePath = "";
        public string s_FileName = "";
        public string strStaffID = "";
        public string strStaffName = "";
        public string strStaffNodesID = "";
        public string strStaffNodesName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            s_FilePath = ConfigurationManager.AppSettings["FileRoot"] + "SlrStaffbooks/";
            i_LessionID = CommRequest.GetInt("LessionID", 0);
            if (!this.InitUserInfo()) return;
            this.InitBookList();
            this.InitPageInfo();
        }

        public void InitBookList()
        {
            DataTable dt_PurchaseInfo = new Catering_Rules.StaffBooks().ViewGetList(" 1=1 AND LessonID=" + i_LessionID + " ").Tables[0];//分页处理
            this.Repeater1.DataSource = dt_PurchaseInfo;
            this.Repeater1.DataBind();
        }

        public bool InitUserInfo()
        {
            if (Session["StaffID"] != null)
            {
                strStaffID = Session["StaffID"].ToString();
                strStaffName = Session["StaffName"].ToString();
                strStaffNodesID = Session["StaffNodesID"].ToString();
                strStaffNodesName = Session["StaffNodesName"].ToString();
                return true;
            }
            return false;
        }

        public void InitPageInfo()
        {
            Catering_Model.StaffLessonsLog _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
            _ModelStaffLessonsLog = new Catering_Rules.StaffLessonsLog().GetModel(i_LessionID, int.Parse(strStaffNodesID), int.Parse(strStaffID));
            this.ddl_Addr.Items[0].Selected = true;
            if (_ModelStaffLessonsLog != null)
            {
                this.ddl_Addr.SelectedValue = _ModelStaffLessonsLog.Addre;
                this.Button1.Enabled = false;
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int BookID = int.Parse(e.CommandArgument.ToString());
            if (BookID <= 0) return;
            if (e.CommandName == "read")
            {
                Catering_Model.StaffBooks _BooksModel = new Catering_Model.StaffBooks();
                _BooksModel = new Catering_Rules.StaffBooks().GetModel(BookID);
                if (_BooksModel == null) return;
                string staff = FileConvert_SWF.strType(_BooksModel.BookAddr);
                s_FileName = s_FilePath + _BooksModel.BookAddr.Replace(staff, ".swf");
            }
        }

        /// <summary>
        /// 开始学习
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Catering_Model.StaffLessonsLog _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
            _ModelStaffLessonsLog = new Catering_Rules.StaffLessonsLog().GetModel(i_LessionID, int.Parse(strStaffNodesID), int.Parse(strStaffID));
            if (_ModelStaffLessonsLog == null)
            {
                _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
                _ModelStaffLessonsLog.Addre = this.ddl_Addr.SelectedValue;
                _ModelStaffLessonsLog.AddTime = DateTime.Now;
                _ModelStaffLessonsLog.AddUserID = int.Parse(strStaffID);
                _ModelStaffLessonsLog.LessonID = i_LessionID;
                _ModelStaffLessonsLog.NodeID = int.Parse(strStaffNodesID);
                _ModelStaffLessonsLog.TestDate = Convert.ToDateTime("1949-10-01");
                _ModelStaffLessonsLog.UserEndDate = Convert.ToDateTime("1949-10-01");
                _ModelStaffLessonsLog.UserID = int.Parse(strStaffID);
                _ModelStaffLessonsLog.UserStartDate = DateTime.Now;
                new Catering_Rules.StaffLessonsLog().Add(_ModelStaffLessonsLog);
            }
            this.Button1.Enabled = false;
            this.Button3.Enabled = true;
        }

        /// <summary>
        /// 结束学习
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button3_Click(object sender, EventArgs e)
        {
            Catering_Model.StaffLessonsLog _ModelStaffLessonsLog = new Catering_Model.StaffLessonsLog();
            _ModelStaffLessonsLog = new Catering_Rules.StaffLessonsLog().GetModel(i_LessionID, int.Parse(strStaffNodesID), int.Parse(strStaffID));
            if (_ModelStaffLessonsLog != null)
            {
                _ModelStaffLessonsLog.UserEndDate = DateTime.Now;
                new Catering_Rules.StaffLessonsLog().Update(_ModelStaffLessonsLog);
            }
            this.Button1.Enabled = false;
            this.Button3.Enabled = false;
        }
    }
}